<?php

function is_logged()
{
	return isset( $_SESSION['group'] );
}

function is_logged_as_monitor()
{
	return is_logged() && ($_SESSION['group'] == 'M');
}

function is_logged_as_volunteer()
{
	return is_logged() && ($_SESSION['group'] == 'V');
}

function is_logged_as_admin()
{
	return isset( $_SESSION['admin'] );
}

function can_post_new_page()
{
	return is_logged() && 
		($_SESSION['group'] == 'L' || $_SESSION['group'] == 'M');
}

function can_delete_file()
{
	return is_logged() && 
		($_SESSION['group'] == 'L' || $_SESSION['group'] == 'M');
}

function get_model($mid)
{
	$sql = "SELECT topic, admin, message, page_number FROM `model` WHERE m_id = $mid";	
	$result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );
	$model = mysql_fetch_array( $result ) or report_err_and_exit( '指定板块不存在。' );

	return $model;
}

function validate_action_get($keys)
{
	validate_action_fields( $keys, $_GET );
}

function validate_action_post($keys)
{
	validate_action_fields( $keys, $_POST );
}

function validate_action_fields($keys, &$container)
{
	foreach( $keys as $key )
	{
		if (!isset( $container[$key] ) || $container[$key] == '' )
			report_err_and_exit( '`' . $key . '`不能为空' );
	}
}

function alert_and_redirect($msg, $go_to_url = 'index.php')
{
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-cn" />
<title>正在重定向...</title>
</head>
<body>
    <script>
        alert("<?php echo $msg; ?>！");
        location.href="<?php echo $go_to_url; ?>";
    </script>
</body>
</html>';
<?php
	exit();
}

function report_err_and_exit($errMsg, $go_to_url = 'index.php')
{
	echo <<<ROD
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-cn" />
<title>出错了...</title>
</head><body>
ROD;

	echo '<script>alert("' . $errMsg . '！");location.href="' . $go_to_url .'";</script></body></html>';
	
	exit();
	
	//---------------------------------------------

	echo <<<ROD
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-cn" />
<title>出错了...</title>
</head><body>
ROD;

	echo '<h1 style="color:red;font-family:Verdana">Error: ', $errMsg, '</h1></body></html>';
	
	exit();
}


function redirect_and_exit($url, $msg, $delay = 1)
{
	echo '<script>location.href="' . $url . '";</script>';
	exit();

	echo <<<ROD
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-cn" />
<meta http-equiv="Refresh" content="$delay;url=$url" />
<title>{$msg}，正在重定向...</title>
</head>
<body>
<h1 style="font-family:Verdana">{$msg}，$delay 秒后自动跳转到&nbsp;<a href="$url">$url</a></h1>
</body></html>
ROD;

	exit();
}

?>